﻿using System.Collections.Generic;
using System.Data;

namespace Donal.Xms.BLL
{
    /// <summary>
    ///订单表
    /// </summary>
    public partial class orders
    {
        private readonly Model.sysconfig sysConfig;
        private readonly DAL.orders dal;

        public orders(bool isRuntime = false)
        {
            sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
            dal = new DAL.orders(sysConfig.sysdatabaseprefix);
        }

        public orders(Model.sysconfig _sysConfig)
        {
            sysConfig = _sysConfig;//获得系统配置信息           
            dal = new DAL.orders(sysConfig.sysdatabaseprefix);
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.orders model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.orders model)
        {
            //计算订单总金额:商品总金额+配送费用+支付手续费            
            //model.real_amount + model.express_fee + model.payment_fee + model.invoice_taxes;
            return dal.Update(model);
        }

        public bool UpdateAddressForChildOrder(Model.orders model)
        {
            return dal.UpdateAddressForChildOrder(model);
        }
                

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return dal.Delete(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.orders GetModel(int id)
        {
            return dal.GetModel(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public string GetValue(string order_no, string fileName)
        {
            return dal.GetValue(order_no, fileName);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public string GetValue(int order_id, string fileName)
        {
            return dal.GetValue(order_id, fileName);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return dal.GetList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetDetailsList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return dal.GetDetailsList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }

        /// <summary>
        /// 获得查询开票分页数据
        /// </summary>
        public DataSet GetInvoiceList(int pageSize, int pageIndex, int company_id, string strWhere, string filedOrder, out int recordCount)
        {
            return dal.GetInvoiceList(pageSize, pageIndex, company_id, strWhere, filedOrder, out recordCount);
        }

        #endregion

        #region 扩展方法================================

        /// <summary>
        /// 获得快递信息
        /// </summary>
        public DataSet GetExpressList(string order_ids)
        {
            return dal.GetExpressList(order_ids);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string order_no)
        {
            return dal.Exists(order_no);
        }

        /// <summary>
        /// 根据订单号返回一个实体
        /// </summary>
        public Model.orders GetModel(string order_no)
        {
            return dal.GetModel(order_no);
        }

        /// <summary>
        /// 根据订单号ParentId查找Order
        /// </summary>
        public List<Model.orders> GetOrderList(int parent_id)
        {
            return dal.GetOrderList(parent_id);
        }

        /// <summary>
        /// 根据订单号ParentId查找Order
        /// </summary>
        public List<Model.orders> GetOrderList(string where)
        {
            return dal.GetOrderList(where);
        }

        /// <summary>
        /// 查找订单下的产品        
        /// </summary>
        /// <param name="order_goods_ids">dt_order_goods表的ID字符串</param>
        public DataSet GetOrderGoodsList(string order_goods_ids, string order_id)
        {
            return dal.GetOrderGoodsList(order_goods_ids, order_id);
        }

        /// <summary>
        /// 根据订单号获取支付方式ID
        /// </summary>
        public int GetPaymentId(string order_no)
        {
            return dal.GetPaymentId(order_no);
        }

        /// <summary>
        /// 返回数据数
        /// </summary>
        public int GetCount(string strWhere)
        {
            return dal.GetCount(strWhere);
        }

        /// <summary>
        /// 修改子订单状态，如果这个订单有子单
        /// </summary>
        public void UpdateField(int parent_id, int status)
        {
            dal.UpdateField(parent_id, status);
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public void UpdateField(int id, string strValue)
        {
            dal.UpdateField(id, strValue);
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public string UpdateErpBuddyField(decimal order_amount, decimal user_amout, decimal user_pay_point, string companyCode, string operators, int _order_id, string orderNo, string remark)
        {
            return dal.UpdateErpBuddyField(order_amount, user_amout, user_pay_point, companyCode, operators, _order_id, orderNo, remark);
        }

        /// <summary>
        ///订单取消流水
        /// </summary>
        public string FlowActionCancelOrder(decimal order_amount, decimal user_pay_point, string companyCode, string operators, int _order_id, string orderNo, string remark, int flowType)
        {
            return dal.FlowActionCancelOrder(order_amount, user_pay_point, companyCode, operators, _order_id, orderNo, remark, flowType);
        }

        /// <summary>
        /// 更新订单状态，并添加流水信息，添加订单日志等操作。
        /// </summary>
        /// <param name="order_no">订单号</param>
        /// <param name="payment">1、手机支付宝；2、电脑支付宝；3、月结；4、账户余额；5、微信公众号；6、微信扫码；
        /// 14、手机支付宝+账户余额；24、电脑支付宝+账户余额；54、微信公众号+账户余额；64、微信扫码+账户余额</param>
        /// <param name="strValue">更新的内容</param>
        /// <param name="order_amount">支付金额，0为订单金额！</param>
        public bool UpdateField(string order_no, string strValue, int payment, decimal order_amount = 0)
        {
            return dal.UpdateField(order_no, strValue, payment, order_amount);
        }

        
        /// <summary>
        /// 获取最后下单时间
        /// </summary>
        public string GetLastOrderTime(int company_id)
        {
            return dal.GetLastOrderTime(company_id);
        }
                

        /// <summary>
        /// 返回限时
        /// </summary>
        public int GetHoldUp()
        {
            return dal.GetHoldUp();
        }

        /// <summary>
        /// 重新分配优惠券
        /// </summary>
        public bool AddTemp(List<Model.order_goods> order_goods_list)
        {
            return dal.AddTemp(order_goods_list);
        }

        /// <summary>
        /// 插入交易号，如果成功，返回真，说明没有处理过此订单。如果假，说明此订单正在处理。
        /// </summary>
        public bool AddTradeNo(string trade_no, string order_no, int payment, decimal order_amount, int notify)
        {
            return dal.AddTradeNo(trade_no, order_no, payment, order_amount, notify);
        }

        /// <summary>
        /// 删除交易号
        /// </summary>
        /// <param name="trade_no">交易号</param>
        public void DeleteTradeNo(string trade_no)
        {
            dal.DeleteTradeNo(trade_no);
        }
        #endregion
    }
}